//
//  GJMapSelectedPointBottmView.m
//  xmgjAppFront
//
//  Created by Zeki on 2020/7/27.
//  Copyright © 2020 guojutech. All rights reserved.
//

#import "GJMapSelectedPointBottmView.h"

@interface GJSELViewCell ()
@property (nonatomic, strong)NSDictionary *poiInfo;

@property (nonatomic, strong)UILabel *poiNameLabel;
@property (nonatomic, strong)UILabel *poiAddressLabel;
@property (nonatomic, strong)UIImageView *rightImageView;
@property (nonatomic, strong) UIView *lineView;

@end

@implementation GJSELViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    NSString *cellID = [NSString stringWithFormat:@"%@ID", NSStringFromClass([self class])];
    GJSELViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  [super setSelected:selected animated:animated];
  if (selected) {
    self.contentView.backgroundColor = HexColor(#FAF5EE);
    self.poiNameLabel.textColor = self.poiAddressLabel.textColor = HexColor(#BA914A);
    self.rightImageView.hidden = NO;
  }else{
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.poiNameLabel.textColor = HexColor(#343332);
    self.poiAddressLabel.textColor = HexColor(#999896);
    self.rightImageView.hidden = YES;
  }
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    [self.contentView addSubview:self.poiAddressLabel];
    [self.contentView addSubview:self.poiNameLabel];
    [self.contentView addSubview:self.rightImageView];
    [self.contentView addSubview:self.lineView];
    
    [self.poiNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.mas_equalTo(16);
      make.height.mas_equalTo(23);
      make.top.mas_equalTo(12);
      make.right.equalTo(self.rightImageView.mas_left).offset(-5);
    }];
    [self.poiAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.right.equalTo(self.poiNameLabel);
      make.bottom.mas_equalTo(-12);
      make.top.equalTo(self.poiNameLabel.mas_bottom).offset(4);
    }];
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.right.mas_equalTo(-16);
      make.height.width.mas_equalTo(20);
      make.centerY.equalTo(self);
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.mas_equalTo(16);
      make.right.mas_equalTo(-16);
      make.height.mas_equalTo(0.5);
      make.bottom.mas_equalTo(-0.5);
    }];
    self.rightImageView.hidden = YES;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
  }
  return self;
}
//- (void)drawRect:(CGRect)rect{
//  [super drawRect:rect];
//  CGContextRef context = UIGraphicsGetCurrentContext();
////  CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
////  CGContextFillRect(context, rect);
//  UIColor *lineColor = HexColor(#E6E6E5);
//  CGFloat lineHeight = 0.5;
//  CGContextSetStrokeColorWithColor(context, lineColor.CGColor);// 线条颜色
//  CGContextSetLineWidth(context, lineHeight);//线条宽度
//  CGContextMoveToPoint(context, 16,rect.size.height - lineHeight); //开始画线, x，y 为开始点的坐标
//  CGContextAddLineToPoint(context, rect.size.width - 16, rect.size.height - lineHeight);//画直线, x，y 为线条结束点的坐标
//  CGContextStrokePath(context); //开始画线
//}

#pragma mark -- getter
- (UILabel *)poiNameLabel{
  if (!_poiNameLabel) {
    _poiNameLabel = [UILabel new];
    _poiNameLabel.textAlignment = NSTextAlignmentLeft;
    _poiNameLabel.textColor = HexColor(#343332);
    _poiNameLabel.font = kMediumSystemFontOfSize(16);
    _poiNameLabel.backgroundColor = [UIColor clearColor];
    
  }
  return _poiNameLabel;
}
- (UILabel *)poiAddressLabel{
  if (!_poiAddressLabel) {
    _poiAddressLabel = [UILabel new];
    _poiAddressLabel.font = kSystemFontOfSize(12);
    _poiAddressLabel.textColor = HexColor(#999896);
    _poiAddressLabel.textAlignment = NSTextAlignmentLeft;
    _poiAddressLabel.backgroundColor = [UIColor clearColor];
  }
  return _poiAddressLabel;
}
- (UIImageView *)rightImageView{
  if (!_rightImageView) {
    _rightImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mapTableSelected"]];
  }
  return _rightImageView;
}
- (UIView *)lineView{
  if (!_lineView) {
    _lineView = [UIView new];
    _lineView.backgroundColor = HexColor(#E6E6E5);
  }
  return _lineView;
}
#pragma mark -- setter
- (void)setPoiInfo:(NSDictionary *)poiInfo{
  _poiInfo = poiInfo;
   self.poiNameLabel.text = poiInfo[@"name"];
   self.poiAddressLabel.text = poiInfo[@"address"];
}

@end



@interface GJMapSelectedPointBottmView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UIButton *button;
@property (nonatomic, strong)NSDictionary *currentInfo;
@end




@implementation GJMapSelectedPointBottmView
//@synthesize dataSource = _dataSource;
- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.tableView];
    [self addSubview:self.button];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.top.left.right.equalTo(self);
      make.bottom.equalTo(self.button.mas_top).offset(-4);
    }];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
      make.height.mas_equalTo(44);
      make.left.mas_equalTo(20);
      make.right.mas_equalTo(-20);
      make.bottom.mas_equalTo(-16);
    }];
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 6;
  }
  return self;
}
- (void)buttonClick:(UIButton *)sender{
  if (self.buttonClickBlock) {
    self.buttonClickBlock(self.currentInfo);
  }
}

- (UIButton *)button{
  if (!_button) {
    _button = [UIButton new];
    [_button setTitle:@"设为装货点" forState:UIControlStateNormal];
    _button.titleLabel.font = kSystemFontOfSize(16);
    _button.backgroundColor = HexColor(#BA914A);
    _button.layer.cornerRadius = 4;
    _button.layer.masksToBounds = YES;
    [_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _button;
}

#pragma mark -- tableViewDelegate&&tableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 72;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  GJSELViewCell *cell = [GJSELViewCell cellWithTableView:tableView];
  cell.poiInfo = self.dataSource[indexPath.row];
  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  if (self.didSelectedBlcok) {
    self.didSelectedBlcok(self.dataSource[indexPath.row]);
  }
  self.currentInfo = self.dataSource[indexPath.row];
}
#pragma mark -- getter
- (UITableView *)tableView{
  if (!_tableView) {
    _tableView = [UITableView new];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
  }
  return _tableView;
}
- (void)setDataSource:(NSArray *)dataSource{
  _dataSource = dataSource;
  [self.tableView reloadData];
  [self gj_hiddenNodataView];
  if (dataSource.count > 0) {
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    [self.tableView scrollToRow:0 inSection:0 atScrollPosition:UITableViewScrollPositionNone animated:YES];
    self.currentInfo = dataSource.firstObject;
  }else{
    [self.tableView gj_showNodataViewWithImage:[UIImage imageNamed:@"mapModule_noneData"] text:@"暂无数据" retryButtonTitle:@"" retryButtonBlock:^{
      
    }];
  }
}
- (void)setButtonTitle:(NSString *)buttonTitle{
  _buttonTitle = buttonTitle;
  [_button setTitle:buttonTitle forState:UIControlStateNormal];
}
@end
